const express = require('express');

// 实体类
// const Department = require('../entity/hospital/Department');
// const User = require('../entity/user/User');
const ObjId = require('../entity/common/ObjId');
const Response = require('../entity/http/Response');
const RepairInfo = require('../entity/repair/RepairInfo');

// 业务层
const repairService = require('../service/repair');
const UpdateRepairInfo = require('../entity/repair/UpdateRepairInfo');

const router = express.Router();

/**
 * @apiName 新增维修信息接口
 */
router.post('/repair', function (req, res, next) {
    console.log(`[ REQUEST ] [ req-id = ${ req.requestId } ] [ Controller ] 请求进入 POST /repair/repair`);
    const { uid } = req.token_payload.data;
    let repair;
    try {
        repair = new RepairInfo(req.body);
    } catch (err) {
        return res.status(400).json(new Response().fail(400, "维修信息有误"));
    }
    console.log(`[ INFO ] [ req-id = ${ req.requestId } ] [ Controller ] 维修信息 ${ JSON.stringify(repair) }`);
    // 进入业务层
    repairService.addRepair(req.requestId, uid, repair).then(result => {
        console.log(`[ RESPONSE ] [ req-id = ${ req.requestId } ] [ Controller ] 新增维修信息响应 ${ JSON.stringify(result) }`);
        return res.status(result.code).json(result);
    }).catch(err => {
        console.log(`[ ERROR ] [ req-id = ${ req.requestId } ] [ Controller ] 新增维修信息时服务器内部错误 ${ err.message }`);
        return res.status(500).json(new Response().fail(500, '服务器内部错误'));
    });
});

/**
 * @apiName 获取所有维修信息接口
 */
router.get('/repairList', function (req, res, next) {
    console.log(`[ REQUEST ] [ req-id = ${ req.requestId } ] [ Controller ] 请求进入 GET /repair/repairList`);
    const { uid } = req.token_payload.data;
    // 进入业务层
    repairService.getAllRepair(req.requestId, uid).then(result => {
        console.log(`[ RESPONSE ] [ req-id = ${ req.requestId } ] [ Controller ] 获取所有维修信息响应 ${ JSON.stringify(result) }`);
        return res.status(result.code).json(result);
    }).catch(err => {
        console.log(`[ ERROR ] [ req-id = ${ req.requestId } ] [ Controller ] 获取所有维修信息时服务器内部错误 ${ err.message }`);
        return res.status(500).json(new Response().fail(500, '服务器内部错误'));
    });
})

/**
 * @apiName 获取维修信息详情接口
 */

/**
 * @apiName 修改维修信息接口
 */
router.put('/repair', function (req, res, next) {
    console.log(`[ REQUEST ] [ req-id = ${ req.requestId } ] [ Controller ] 请求进入 PUT /repair/repair`);
    const { uid } = req.token_payload.data;
    let repair;
    try {
        repair = new UpdateRepairInfo(req.body);
    } catch (err) {
        return res.status(400).json(new Response().fail(400, "维修信息有误"));
    }
    console.log(`[ INFO ] [ req-id = ${ req.requestId } ] [ Controller ] 维修信息 ${ JSON.stringify(repair) }`);
    // 进入业务层
    repairService.updateRepair(req.requestId, uid, repair).then(result => {
        console.log(`[ RESPONSE ] [ req-id = ${ req.requestId } ] [ Controller ] 修改维修信息响应 ${ JSON.stringify(result) }`);
        return res.status(result.code).json(result);
    }).catch(err => {
        console.log(`[ ERROR ] [ req-id = ${ req.requestId } ] [ Controller ] 修改维修信息时服务器内部错误 ${ err.message }`);
        return res.status(500).json(new Response().fail(500, '服务器内部错误'));
    });
});

/**
 * @apiName 删除维修信息接口
 */
router.delete('/repair', function (req, res, next) {
    console.log(`[ REQUEST ] [ req-id = ${ req.requestId } ] [ Controller ] 请求进入 DELETE /repair/repair`);
    const { uid } = req.token_payload.data;
    let repair;
    try {
        repair = new ObjId(req.query.id).id;
    } catch (err) {
        return res.status(400).json(new Response().fail(400, "维修信息有误"));
    }
    console.log(`[ INFO ] [ req-id = ${ req.requestId } ] [ Controller ] 维修信息 ${ JSON.stringify(repair) }`);
    // 进入业务层
    repairService.deleteRepair(req.requestId, uid, repair).then(result => {
        console.log(`[ RESPONSE ] [ req-id = ${ req.requestId } ] [ Controller ] 删除维修信息响应 ${ JSON.stringify(result) }`);
        return res.status(result.code).json(result);
    }).catch(err => {
        console.log(`[ ERROR ] [ req-id = ${ req.requestId } ] [ Controller ] 删除维修信息时服务器内部错误 ${ err.message }`);
        return res.status(500).json(new Response().fail(500, '服务器内部错误'));
    });
});

module.exports = router;